package logic

import (
	"context"
	"gorm.io/gorm"
	"rent/model/mysql"

	"rent/rentrpc/internal/svc"
	"rent/rentrpc/rentrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddLogic) Add(in *rentrpc.AddRequest) (*rentrpc.AddResponse, error) {
	// todo: add your logic here and delete this line

	product := mysql.Product{
		Model:       gorm.Model{},
		Produce:     in.Produce,
		Description: in.Description,
		Price:       int(in.Price),
		UserId:      int(in.UserId),
	}

	mysql.DB.Debug().Model(&product).Create(&product)

	return &rentrpc.AddResponse{Id: int64(product.UserId)}, nil
}
